package com.ming.controller;

import com.ming.dao.CustomerMapper;
import com.ming.dao.StaffMapper;
import com.ming.pojo.Customer;
import com.ming.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class StaffController {
    @Autowired
    StaffMapper staffMapper;

    @RequestMapping("/staffs")
    public String list(Model model){
        List<Staff> staffs = staffMapper.queryStaffs();
        model.addAttribute("staffs",staffs);
        return "staffs/list";
    }

    @GetMapping("/staff")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<Staff> staffs = staffMapper.queryStaffs();
        model.addAttribute("staffs",staffs);
        return "staffs/add";
    }

    @PostMapping("/staff")
    public String addStaff(Staff staff){
        staff.setSid(staffMapper.getMId());
        staffMapper.addStaff(staff);
        return "redirect:/staffs";
    }

    //去客户的修改页面
    @GetMapping("/staff/{id}")
    public String toUpdateStaff(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        Staff staff = staffMapper.queryStaffById(id);
        model.addAttribute("staff", staff);
        return "staffs/update";
    }

    @PostMapping("/updateStaff")
    public String updateStaff(Staff staff) {
        staffMapper.updateStaff(staff);
        return "redirect:/staffs";
    }


    @GetMapping("/deleteStaff/{id}")
    public String deleteStaff(@PathVariable("id")Integer id){
        staffMapper.deleteStaff(id);
        return "redirect:/staffs";
    }

}
